<?php
/**
 * 格式化输出 基类
 * @author Guofeng
 * @date 2021-07-12
 */

namespace App\Http\Format;

class FormatBase
{

    /**
     * 效验字符串
     * 效验请求过来的参数 是否存在或是否为空
     * @param string $key
     * @param array $array
     * @return bool
     */
    protected static function isExistsStr($key, $array = [])
    {
        if ($key && $array && !array_key_exists($key, $array)) {
            return false;
        }
        $str = trim($array[$key]);
        return !empty($str) ? true : false;
    }

    /**
     * 效验数据
     * 效验请求过来的数据 是否存在或是否为空
     * @param string $key
     * @param array $array
     * @return bool
     */
    public static function isExistsArray($key, $array = [])
    {
        if (!array_key_exists($key, $array)) {
            return false;
        }
        return !empty($array[$key]) ? true : false;
    }


    /**
     * 格式化手机号码
     * @param $mobile
     * @return string|string[]|null
     */
    public static function formatMobile($mobile)
    {
        $strReturn = '';
        if (strlen(trim($mobile)) == 11) {
            $strReturn = preg_replace('/(\d{3})\d{4}(\d{4})/', '$1****$2', $mobile);
        }
        return $strReturn;
    }

    /**
     * format格式化返回视图层数据
     * $data => [
     *   'list' => [
     *     '0' => [
     *        'name'  => 'guofeng',
     *     ],
     *   ],
     *   'page_data' => [
     *      'page' => '1',
     *    ],
     * ],
     * 该方法只依据 _为依据 驼峰转换命名
     * @param array $arrParams
     * @return array
     */
    public static function formatArray(array $arrParams)
    {

        $arrReturn = [];
        #校验参数
        if (is_array($arrParams) && count($arrParams) > 0) {

            #判断是几维数组 目前支持三维
            $intArrDimension = self::checkArrayDimension($arrParams);

            #校验必须为数组
            if ($intArrDimension == 0) return false;

            #最多支持3维数组
            if ($intArrDimension > 3) return false;

            #循环Format值
            foreach ($arrParams as $arrKey => $arrVal) {
                #校验key值是否存在_
                $strArrKey = $arrKey;
                $explodeFormatKeyArr = explode('_', $strArrKey);
                if ($explodeFormatKeyArr > 1) {
                    #key值存在_
                    if (count($explodeFormatKeyArr) > 1) {
                        $strKey = "";
                        #将format的key进行format化
                        foreach ($explodeFormatKeyArr as $explodeFormatKey => $explodeFormatVal) {
                            if ($explodeFormatKey > 0) {
                                $strKey .= ucfirst($explodeFormatVal);
                            } else {
                                $strKey = $explodeFormatVal;
                            }
                        }
                        $strArrKey = $strKey;
                    }
                }
                #定义Format化数组
                $formatArr = $arrVal;
                #校验Format的数组层级
                $intFormatArrDimension = self::checkArrayDimension($formatArr);
                if ($intFormatArrDimension == 2) {
                    #二维数组调用format方法
                    foreach ($formatArr as $formatVal) {
                        $arrReturn[$strArrKey][] = self::assembleKey($formatVal);
                    }
                } else {
                    #一维数组调用format方法
                    $arrReturn[$strArrKey] = self::assembleKey($formatArr);
                }
            }
            #释放变量
            unset($intArrDimension);
        }
        return $arrReturn;
    }


    /**
     *  Action 组装数组键名 （eg:task_id 改为 taskId 驼峰命名）assembleKey
     *  @params $arrParams
     *  @return array
     * $arrParam = [
     *   'company_name' => 'guofeng',
     *   'posting_no' => 'JD1300001',
     * ];
     * @param $arrParams
     * @return array|false
     */
    public static function assembleKey($arrParams)
    {
        $arrReturn = [];
        if ($arrParams) {
            $keyArr = [];
            $valArr = [];
            //将_转换为首字母大写
            foreach ($arrParams as $key => $val) {
                $explodeKeyArr = explode('_', $key);
                if (count($explodeKeyArr) > 0) {
                    $strKey = "";
                    foreach ($explodeKeyArr as $explodeKey => $explodeVal) {

                        if ($explodeKey > 0) {
                            $strKey .= ucfirst($explodeVal);
                        } else {
                            $strKey = $explodeVal;
                        }
                    }

                    $keyArr[] = $strKey;
                }
                $valArr[] = $val;
            }
            $arrReturn = array_combine($keyArr, $valArr);
        }
        return $arrReturn;
    }


    /**
     * Action 组装数组键名 （eg:task_id 改为 taskId 驼峰命名）assembleKey
     * @param $array
     * @return int
     */
    public static function checkArrayDimension($array)
    {
        #校验是否为数组 不是数组直接返回 0
        if (!is_array($array)) return 0;
        else {
            $max = 0;
            foreach ($array as $item) {
                $intNum = self::checkArrayDimension($item);
                if ($intNum > $max) $max = $intNum;
            }
            return $max + 1;
        }
    }


    /**
     * Action Format 列表统一格式
     * @param $data
     * @return array
     */
    public static function listData($data =[]){
        $returnData['list'] = [];
        //判断是否为数组
        if(is_array($data) && array_key_exists('data',$data)){
            $returnData['list'] = $data['data'];
            //调用分页
            $pagePage = self::pageData($data);
            if(!empty($pagePage)){
                $returnData['pageData'] = $pagePage['pageData'];
            }
            unset($data);
        }
        return $returnData;
    }

    /**
     * Action Format 分页统一格式
     * @param $data
     * @return array
     */
    public static function pageData($data){
        $pageData['pageData'] =[
            'count' => 0,
            'page' => 1,
        ];
        $returnData = [];
        if(array_key_exists('current_page',$data) && is_array($data)){
            $pageData['pageData']['count'] = $data['total'];
            $pageData['pageData']['page'] = $data['current_page'];
            $pageData['pageData']['handle_count'] = $data['handle_total'] ?? 0;
            $returnData = $pageData;
        }
        unset($data);
        return $returnData;
    }

}
